﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace pos
{
    public partial class MainMDIParent : Form
    {
      //  private int childFormNumber = 0;
        private Hashtable childForms;
        private WebServiceForm webServiceFrom=null;
        public MainMDIParent()
        {
            InitializeComponent();
            childForms = new Hashtable();
        }

        public void showFormByChild(string fname,string mname,string para)
        {
            // 创建此子窗体的一个新实例。

            string strForm = fname;
            Form childForm = GetForm(fname);

               


            this.Text = strForm;
            childForm.WindowState = FormWindowState.Maximized;
            childForm.Show();

            System.Reflection.MethodInfo mi;

            mi = childForm.GetType().GetMethod(mname, new Type[] { typeof(string) });
            if (mi != null)
                mi.Invoke(childForm, new object[] {para });

        }
        private Form GetForm(string strForm)
        {
            Form childForm = (Form)childForms[strForm];
            if (childForm == null || childForm.MdiParent == null)
            {
                if (childForm != null)
                    childForms.Remove(strForm);
                switch (strForm)
                {
                    case "商品管理":
                        {
                            childForm = new CommodityFrm(null);

                            break;
                        }
                    case "入库管理":
                        {
                            childForm = new StorageFrm(null);
                            break;
                        }
                    case "出库管理":
                        {
                            childForm = new ReturnedAdminFrm();
                            break;
                        }
                    case "销售管理":
                        {
                            childForm = new saleAdminFrm(null);
                            break;
                        }
                    case "库存查询":
                        {
                            childForm = new QuickQuery(null);
                            break;
                        }
                    case "商品工具":
                        {
                            childForm = new CommodityTool(null);
                            break;
                        }
                    case "供应商管理":
                        {
                            childForm = new ParaDealerFrm();
                            break;
                        }
                    default:
                        return null;
                }

                childForm.MdiParent = this;
                childForms.Add(strForm, childForm);
               
            }
            return childForm;
        }
        private void ShowNewForm(object sender, EventArgs e)
        {
            // 创建此子窗体的一个新实例。
            
            string strForm = sender.ToString();
            strForm=strForm.Split(new char[]{'('})[0];
            

                
            Form childForm = GetForm(strForm);
            this.Text = strForm;
            childForm.WindowState = FormWindowState.Maximized;
            childForm.Show();
        }

        private void OpenFile(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            openFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = openFileDialog.FileName;
                // TODO: 在此处添加打开文件的代码。
            }
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
                // TODO: 在此处添加代码，将窗体的当前内容保存到一个文件中。
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO: 使用 System.Windows.Forms.Clipboard 将所选的文本或图像插入到剪贴板
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO: 使用 System.Windows.Forms.Clipboard 将所选的文本或图像插入到剪贴板
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // TODO: 使用 System.Windows.Forms.Clipboard.GetText() 或 System.Windows.Forms.GetData 从剪贴板中检索信息。
        }

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStrip.Visible = toolBarToolStripMenuItem.Checked;
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            settingFrm frm = new settingFrm();
            frm.ShowDialog();
        }

        private void MainMDIParent_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F1:
                    ShowNewForm("商品管理", null);
                    break;
                case Keys.F2:
                    ShowNewForm("入库管理", null);
                    break;
                case Keys.F3:
                    ShowNewForm("库存查询", null);
                    break;

                case Keys.F4:
                    ShowNewForm("出库管理", null);
                    break;
                case Keys.F5:
                    ShowNewForm("销售管理", null);
                    break;
               
            }
        }

        private void MainMDIParent_Load(object sender, EventArgs e)
        {

            if (pos.entity.consts.groupID == "stocker")
            {
                ArrayList list = new ArrayList();
                bool flag = false;
                foreach(ToolStripItem item in toolStrip.Items)
                {
                    if (flag )
                    {
                        list.Add(item);
                        flag = false;
                        continue;
                    }
                    if (item.Text == "销售管理"||item.Text== "库存查询")
                    {
                        list.Add(item);
                        flag=true;
                        
                    }
                    
                   
                }
                foreach (ToolStripItem item in list)
                {
                    toolStrip.Items.Remove(item);
                }
            }
        }

        private void WebServiceTool_Click(object sender, EventArgs e)
        {
            if (webServiceFrom == null || webServiceFrom.MdiParent == null)
            {
                webServiceFrom = new WebServiceForm();
                webServiceFrom.MdiParent = this;
            }
            webServiceFrom.WindowState = FormWindowState.Maximized;
            webServiceFrom.Show();
        }

        private void print_note_Click(object sender, EventArgs e)
        {

        }

      

       
    }
}
